<?php

namespace App\Admin\Controllers;

use App\Admin\Actions\DownloadTemplate;
use App\Admin\Forms\PackageImport;
use App\Admin\Forms\PackageUsedImport;
use App\Admin\Repositories\CarPackageOrder;
use App\Admin\Services\AdminUserService;
use App\Models\Brand;
use App\Models\CarPackage;
use App\Models\CarPackageCategory;
use App\Models\CarPackageOrderAttr;
use App\Models\Store;
use App\Services\LifeCycle\OrderService;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\OperationLog\Models\OperationLog;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use Dcat\Admin\Widgets\Modal;
use Illuminate\Support\Facades\Auth;

class CarPackageOrderExamineController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(CarPackageOrderAttr::with(['order'])->whereHas('order'), function (Grid $grid){
            $grid->model()->groupBy('user_id');
            $grid->model()->orderBy('order_id', 'desc');

            $package_id = AdminUserService::permissionOrders(Auth::guard('admin')->user()->username);

            if ($package_id) {
                $grid->model()->whereIn('package_id', $package_id);
            }

            $grid->column('order.user_name', admin_trans('car-package-order-examine.fields.user_name'))->display(function () {
                return $this->order->user_name."<br/>{$this->order->user_phone}";
            });

            $grid->column('package_used')->display(function () {
                $package_used = CarPackageOrderAttr::where('order_id', $this->order_id)->where('used_number', '!=', 0)->get()->toArray();
                $str = '';

                if ($package_used) {
                    foreach ($package_used as $key => $value) {
                        $str .= '<div style="margin-top: 5px">'.$value['attr_title']. '<span style="margin-left: 5px;color: #fff;background-color: #52c41a;display: inline-block;
                        padding: 4px 10px;font-size: 12px;line-height: 1;border-radius: 3px;font-weight: 700;"> x' .$value['used_number'] .'</span></div>';
                    }
                }

                return $str;
            })->width('210px');
            $grid->column('package_usable')->display(function () {
                $package_used = CarPackageOrderAttr::where('order_id', $this->order_id)->where('usable_number', '!=', 0)->get()->toArray();
                $str = '';

                if ($package_used) {
                    foreach ($package_used as $key => $value) {
                        $str .= '<div style="margin-top: 5px">' . $value['attr_title'] . '<span style="margin-left: 5px;color: #fff;background-color: #52c41a;display: inline-block;
                        padding: 4px 10px;font-size: 12px;line-height: 1;border-radius: 3px;font-weight: 700;"> x' . $value['usable_number'] . '</span></div>';
                    }
                }

                return $str;
            })->width('210px');

            $grid->filter(function (Grid\Filter $filter) {
                $filter->like('order.user_name', admin_trans('car-package-order-examine.fields.user_name'));
                $filter->like('order.user_phone', admin_trans('car-package-order-examine.fields.user_phone'));
            });

            $grid->disableViewButton();
            $grid->disableDeleteButton();
            $grid->disableCreateButton();
            $grid->disableEditButton();
        });
    }

}
